package com.styytech.dadabao.widge.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;

/**
 * Created by mac on 15/12/8.
 */
public class JobNumDialog {
    private Dialog mDialog;
    private TextView tv_dialog_text,tv_dialog_name;
    private Button btn_ensure;
    private Context mContext;
    private View view;

    private String name;
    //按钮上的字
    private String btn_ensure_text,btn_cancel_text;
    private RelativeLayout llyt_dialog_title;
    /**
     * 判断button的个数*/
    private int type=0;
    Dialogcallback ensuredialogcallback,canceldialogcallback;
    /**
     * 标准样式的确定取消dialog
     * 上下文对象、title、内容、第一个button的文字，第二个button的文字*/
    public JobNumDialog (Context context,String name){
        this.mContext=context;

        this.name=name;
        initViews();
    }


    private void initViews(){
        mDialog = new Dialog(mContext,
                R.style.ensure_dialog);
        LayoutInflater layoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.dialog_jobnum, null);
        llyt_dialog_title=(RelativeLayout)view.findViewById(R.id.llyt_dialog_title);
        tv_dialog_text=(TextView)view.findViewById(R.id.tv_dialog_text);
        tv_dialog_name=(TextView)view.findViewById(R.id.tv_dialog_name);
        btn_ensure=(Button)view.findViewById(R.id.btn_ensure);

        btn_ensure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ensuredialogcallback.dialogdo();
            }
        });

        /**
         * 根据用户传递的参数，确定布局控件的显示*/
        setVisible();
    }
    private void setVisible(){
       if (name.equals("")){
           tv_dialog_name.setVisibility(View.GONE);
           tv_dialog_text.setText("该工号未同步或工号错误");
           btn_ensure.setText("确认");
       }else{
           tv_dialog_name.setVisibility(View.VISIBLE);
           tv_dialog_text.setText("该工号的员工姓名为");
           tv_dialog_name.setText(name);
           btn_ensure.setText("确认");
       }
        setView();


    }
    /**
     * 设置点击屏幕或者back返回键能否取消*/
    public void NotCancel(boolean cancel){
        mDialog.setCanceledOnTouchOutside(cancel);//false设置点击屏幕其他地方，dialog不消失
        mDialog.setCancelable(cancel);//false设置点击返回键和HOme键，dialog不消失
    }
    public void show(){
        mDialog.show();
    }

    public void dismiss(){
        mDialog.dismiss();
    }

    /**
     * 设定一个interfack接口,使mydialog可以處理activity定義的事情
     * @author sfshine
     *
     */
    public interface Dialogcallback {
        public void dialogdo();
    }
    public void setEnsureDialogCallback(Dialogcallback dialogcallback) {
        this.ensuredialogcallback = dialogcallback;
    }
    public void setCanceleDialogCallback(Dialogcallback dialogcallback) {
        this.canceldialogcallback = dialogcallback;
    }
    private void setView(){
        //mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        mDialog.setContentView(view);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager m = ((Activity)mContext).getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        //  p.height = (int) (d.getHeight() * 0.25); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(lp);

    }
}

